package com.shoe.controller;

import com.github.pagehelper.PageInfo;
import com.shoe.pojo.ShoeGood;
import com.shoe.pojo.ShoeOrder;
import com.shoe.service.ShoeGoodsService;
import com.shoe.vo.ResponseResult;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("nb")
@CrossOrigin
public class ShoeController {
    @Autowired
    ShoeGoodsService shoeGoods;


    @GetMapping("nbda")
    @ResponseBody
    public List<ShoeGood> selectAlll(){

        System.out.println("111111");
        List<ShoeGood> shoeGood = shoeGoods.selectAll();
        return shoeGood;
    }

    @RequestMapping("time")
    @ResponseBody
    public List<ShoeGood> selectA(String age,String goodName,String brand){

        ShoeGood shoeGood = new ShoeGood();
        System.out.println(goodName);
        if(goodName !=""){
            shoeGood.setGoodName(goodName);
        }
        if (brand != "null"){
            shoeGood.setGoodBrand(brand);
        }

        List<ShoeGood> list = shoeGoods.selectPart(shoeGood,age);


        return list;
    }

    @RequestMapping("xiuupdate")
    @ResponseBody
    public String updatePart(String id){
        System.out.println(id);
        shoeGoods.updatepart(Long.valueOf(id));
        return "修改成功";
    }




    @GetMapping("nbdaa")
    @ResponseBody
    public ResponseResult selectAlll(@RequestParam int currPage,@RequestParam int pageSize){

//        System.out.println("333");
        ResponseResult shoeGood = shoeGoods.selectAll(currPage , pageSize);
        return shoeGood;
    }

    @GetMapping("nbdab")
    @ResponseBody
    public ResponseResult selectFen(@RequestParam int currPage,@RequestParam int pageSize, HttpServletResponse response){
        ResponseResult shoeGood = shoeGoods.selectAll(currPage , pageSize);
        return shoeGood;
    }

    @RequestMapping("nnb/{goodId}")
    @ResponseBody
    public ShoeGood selectOne(@PathVariable Long goodId){
        ShoeGood shoeGood = shoeGoods.selectOne(goodId);
        return shoeGood;
    }

    @GetMapping("shoe")
    @ResponseBody
    public ResponseResult selectOther(@RequestParam int currPage,@RequestParam int pageSize ,@RequestParam String goodBrand,@RequestParam String goodSex,@RequestParam String goodScience,@RequestParam String goodUpper,@RequestParam String goodName){
        System.out.println("3333");
        System.out.println(goodBrand);
        ShoeGood shoeGood = new ShoeGood();

        if(!goodSex.equals("undefined")){
            shoeGood.setGoodSex(goodSex);
        }
        if(!goodUpper.equals("undefined")){
            shoeGood.setGoodUpper(goodUpper);

        }
        if(!goodScience.equals("undefined")){
            shoeGood.setGoodScience(goodScience);

        }
        if(!goodBrand.equals("undefined")){
            shoeGood.setGoodBrand(goodBrand);
        }

        if(!goodName.equals("undefined")){
            shoeGood.setGoodName(goodName);
        }
        System.out.println(shoeGood.getGoodSex());
        ResponseResult responseResult = shoeGoods.selectOther(currPage,pageSize,shoeGood);
        return responseResult;
    }

    @RequestMapping("selectById")
    @ResponseBody
    public ShoeGood selectById(@RequestParam String id){
        ShoeGood shoeGood = new ShoeGood();
        shoeGood = shoeGoods.selectById(Long.valueOf(id));
        return shoeGood;
    }

    //添加商品
    @RequestMapping("addgoods")
    @ResponseBody
    public String addGoods(HttpServletRequest request,String goodName, String goodMoney, String goodBrand, String goodUpper, String goodMaterial, String goodScience, String goodFamily, String goodSex,@RequestParam("file")  MultipartFile file) throws IOException, ServletException {
        ShoeGood shoeGood = new ShoeGood();
        shoeGood.setGoodScience(goodScience);
        shoeGood.setGoodSex(goodSex);
        shoeGood.setGoodBrand(goodBrand);
        shoeGood.setGoodName(goodName);
        shoeGood.setGoodMoney(Long.valueOf(goodMoney));
        shoeGood.setGoodUpper(goodUpper);
        shoeGood.setGoodMaterial(goodMaterial);
        shoeGood.setGoodFamily(goodFamily);
        Date date = new Date();
        shoeGood.setGoodDate(date);
        shoeGood.setGoodFamily("火云鞋神");
//        shoeGood.setGoodPhoto(goodPhoto);
        /*Long l = Long.valueOf(goodMoney);*/
        System.out.println(goodBrand);
//        System.out.println(goodPhoto);
//        System.out.println(l);
//        System.out.println(file);
        String fileName = file.getOriginalFilename();
        String targetFilePath = "H:\\project\\photo";
        File myfile = new File(targetFilePath + File.separator + fileName);
        file.transferTo(myfile);
//        System.out.println(fileName);
        String b = "../photo/" + fileName;
        System.out.println(b);
        shoeGood.setGoodPhoto(b);
        shoeGoods.addGoods(shoeGood);

        return "添加成功";

    }


    @RequestMapping("selectAllPage")
    @ResponseBody
    public ResponseResult selectPage(HttpServletRequest request){
        Integer page = Integer.parseInt(request.getParameter("page"));
        Integer limit = Integer.parseInt(request.getParameter("limit"));
        List<ShoeGood> list = shoeGoods.selectAll();
        List<ShoeGood> shoeOrderList = shoeGoods.selectPage((page-1)*limit,limit);
        ResponseResult responseResult = new ResponseResult();
        if (shoeOrderList != null){
            responseResult.setCode(0);
            responseResult.setMsg("查询成功");
            responseResult.setCount(list.size());
            responseResult.setData(shoeOrderList);
        }else {
            responseResult.setCode(2);
            responseResult.setMsg("查询失败");
        }
        return responseResult;
    }








}
